/**
 * Created by lwy on 2017/6/15.
 */

var page=0;
var num;

$(function(){
    doAjax();
})

function doAjax(){
    page++;
    $.ajax({
        url:'/tiXianC/userGetTiXianShenQingList.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            page:page,
        },
        type:'post',
        beforeSend:function(){
            if(page==1){
                $('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('.withdraw-box'));
            }
        },
        success:function(data){
            if(data.code==-1){
                window.location.href="login.html";
            }
            if(data.code==0){
                mui.toast(data.msg);
            }
            if(data.code==1){
                var data = data.datas;
                if(page==1){
                    $('.withdraw-box').empty();
                    if(data.length==0){
                        mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
                        $('<div class="kong"><img src="../phoneImg/kong.png" alt="暂无数据"></div>').appendTo($('.withdraw-box'));
                    }
                }
                for(var i=0;i<data.length;i++){
                    var state = data[i].stateValue;
                    var StateValue;
                    if(state!=1){
                        StateValue='color:#49c9bc';
                    }else{
                        StateValue='color:#e44840';
                    }
                    $('<li money="'+data[i].money+'" state="'+data[i].state+'" accountName="'+data[i].accountName+'" code="'+data[i].code+'" cardNum="'+data[i].cardNum+'" createTime="'+data[i].createTime+'" handTime="'+data[i].handTime+'" factorage="'+data[i].factorage+'" bankName="'+data[i].bankName+'">'+
                        '<p class="record-state" style="'+StateValue+'">'+data[i].state+'</p>'+
                        '<p class="record-card"><span>订单号：'+data[i].code+'</span></p>'+
                        '<div class="record-time"><div>'+data[i].createTime+'</div></div>'+
                        '<div class="record-money" style="'+StateValue+'">-'+data[i].money+'</div>'+
                        '</li>').appendTo($('.withdraw-box'));
                }
                num=data.length;
            }
        }
    })
    return num;
}

//上拉加载
mui.init({
    pullRefresh : {
        container:"#refreshContainer",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
        up : {
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback :function(){
                setTimeout(function () {
                    var numResult = doAjax();;
                    if (numResult < 20) {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                    } else {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
                    }
                }, 1000);
            } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
        }
    }
});

//点击进入详情
mui('body').on('tap','li',function(){
    var money = $(this).attr('money');
    var state = $(this).attr('state');
    var accountName = $(this).attr('accountName');
    var code = $(this).attr('code');
    var cardNum = $(this).attr('cardNum');
    var factorage = $(this).attr('factorage');
    var bankName = $(this).attr('bankName');
    var createTime = $(this).attr('createTime');
    var handTime = $(this).attr('handTime');
    window.location.href=encodeURI(encodeURI("withdraw-detail.html?money="+money+"&state="+state+"&accountName="+accountName+"&code="+code+"&cardNum="+cardNum+"&factorage="+factorage+"&bankName="+bankName+"&createTime="+createTime+"&handTime="+handTime))
})
